/*
 * @Author       : mark
 * @Date         : 2020-06-16
 * @copyleft Apache 2.0
 */
#ifndef SQLCONNPOOL_H
#define SQLCONNPOOL_H

#include <mysql/mysql.h>
#include <string>
#include <queue>
#include <mutex>
#include <semaphore.h>
#include <thread>
#include "../log/log.h"

class SqlConnPool
{
    public:
        
        static SqlConnPool *Instance();

        MYSQL *GetConn();
        void FreeConn(MYSQL *conn);
        int GetFreeConnCount();

        void Init(const char *host, int port,
                const char *user, const char *pwd,
                const char *dbName, int connSize);
        void ClosePool();

    private:

        SqlConnPool();
        ~SqlConnPool();
        int max_conn_;          
        int use_count_;
        int free_count_;

        std::queue<MYSQL *> conn_que_;      
        std::mutex          mtx_;           
        sem_t               sem_;        
};

#endif // SQLCONNPOOL_H